Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

285
Vistas
"yield" overwrites "yield from" items
<?php
function generator1(): \Traversable
{
    yield 'alpha';
    yield 'beta';
    yield 'delta';
}

function generator2(): \Traversable
{
    yield from generator1();
    yield 'upsilon';
    yield 'omega';
}

function resolve(iterable $generator) {
    foreach ($generator as $index => $item) 
        echo "$index $item" . PHP_EOL;
}

resolve(generator1());
echo  PHP_EOL;

resolve(generator2());
die;

Produces this result:

0 alpha
1 beta
2 delta

0 alpha
1 beta
2 delta
0 upsilon
1 omega

That last stream should be:

0 alpha
1 beta
2 delta
3 upsilon
4 omega

What's going on here? Is this a bug in PHP (I'm running v7.4.10 in Ubuntu.) How do I get the correct result without using an array or some other form of collector?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

The result is a specialty of the iterator_to_array() function. The second parameter must be set to false so that all values are recorded as already noted in the comment.

This is also specifically pointed out in the PHP manual.

function generator1(): \Traversable
{
    yield 'alpha';
    yield 'beta';
    yield 'delta';
    yield 'gamma';
    yield 'epsilon';
    yield 'eta';
}

function generator2(): \Traversable
{
    yield from generator1();
    yield 'upsilon';
    yield 'omega';
}

var_dump(iterator_to_array(generator2()));

var_dump(iterator_to_array(generator2(), false));

Result:

array(6) {
  [0]=>
  string(7) "upsilon"
  [1]=>
  string(5) "omega"
  [2]=>
  string(5) "delta"
  [3]=>
  string(5) "gamma"
  [4]=>
  string(7) "epsilon"
  [5]=>
  string(3) "eta"
}
array(8) {
  [0]=>
  string(5) "alpha"
  [1]=>
  string(4) "beta"
  [2]=>
  string(5) "delta"
  [3]=>
  string(5) "gamma"
  [4]=>
  string(7) "epsilon"
  [5]=>
  string(3) "eta"
  [6]=>
  string(7) "upsilon"
  [7]=>
  string(5) "omega"
}

Try for yourself in the sandbox.

This becomes clear when keys and values are output with foreach.

foreach(generator2() as $key => $value){
  var_dump($key,$value);
}

Output:

int(0) string(5) "alpha"
int(1) string(4) "beta"
int(2) string(5) "delta"
int(3) string(5) "gamma"
int(4) string(7) "epsilon"
int(5) string(3) "eta"
int(0) string(7) "upsilon"
int(1) string(5) "omega"
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda